package test0821;

import java.util.Arrays;

/**
 * leecode 16题
 */


public class test {
    public static int threeSumClosest(int[] nums, int target) {
        int best = 100000;
        Arrays.sort(nums);
        for (int i = 0, m = nums.length; i < m ; i++) {
            int left = i + 1;
            int right = m - 1;
            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum == target) {
                    return target;
                }
                if (Math.abs(sum - target) < Math.abs(best-target)){
                    best = sum;
                }
                if(sum>target){
                    int right1 = right-1;
                    while(left<right1&&nums[right1]==nums[right]){
                        right1--;
                    }
                    right = right1;
                }

                if(sum<target){
                    int left1 = left+1;
                    while(left1<right&&nums[left1]==nums[left]){
                        left1++;
                    }
                    left = left1;
                }
            }
        }
        return  best;

//   int best = 100000;
//                List<Integer> list = Arrays.stream(nums).boxed().collect(Collectors.toList()).stream().sorted()
//                        .forEach(i->{
//                            int left = i + 1;
//                            int right = m - 1;
//                            while (left < right) {
//                                int sum = nums[i] + nums[left] + nums[right];
//                                if (sum == target) {
//                                    return target;
//                                }
//                                if (Math.abs(sum - target) < Math.abs(best-target)){
//                                    best = sum;
//                                }
//                                if(sum>target){
//                                    int right1 = right-1;
//                                    while(left<right1&&nums[right1]==nums[right]){
//                                        right1--;
//                                    }
//                                    right = right1;
//                                }
//
//                                if(sum<target){
//                                    int left1 = left+1;
//                                    while(left1<right&&nums[left1]==nums[left]){
//                                        left1++;
//                                    }
//                                    left = left1;
//                                }
//                            }
//                        })
//                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        int[] nums = new int[]{-1, 2, 1, -4,5};
        System.out.println(test.threeSumClosest(nums,11));
    }
}
